package hr.rred;
import processing.core.*; 

import processing.xml.*; 

import processing.pdf.*; 

import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.Map.Entry;
import java.util.regex.*; 

public class tmapData3 extends PApplet {


Table t;
TreeMap<String, Integer> tm;
int rowCount, labelCount;
int count, col, column, time;
float percent;
String title, title2;
PFont fontA, fontB;


public void setup(){
  size(480, 620); 
	
  //size(200, 200, PDF, "out.pdf"); 
  // t = new Table("Reclass-08-09.tsv");
  // t = new Table("Equities-08-09.tsv");
  // t = new Table("wf-08-09.tsv");
  // t = new Table("EMP-apps-08-09.tsv");
  // t = new Table("EMP-Hires-08-09.tsv");
   t = new Table("EMP-salary-placement.tsv");
  // t = new Table("Separations-08-09.tsv");
  //t = new Table("EMP-Time-To-Fill.tsv");
   
  //fontA = loadFont("Ziggurat-HTF-Black-32.vlw");
  fontA = loadFont("Candara-Bold-32.vlw");
  fontB = loadFont("BerlinSansFB-Reg-18.vlw");   
}

public void draw(){
  tm = new TreeMap<String, Integer>();
  crunchData(column); 
  // This is green   fill(#577737);
   fill(245);
  rect(0,0,width,height);
  displayText(tm);
}

public void displayText(TreeMap<String, Integer> hm){
  fill(25,175);
  textFont(fontA, 32);
  text(t.getString(0,column),15,40);
  Iterator<Entry<String, Integer>> i = hm.entrySet().iterator();  // Get an iterator
  int s=0;
  while (i.hasNext()){
    Map.Entry me = (Map.Entry)i.next();
    Integer value = (Integer)me.getValue(); // Retrieve the value as Integer (obj)
    percent = ((float)value/(float)rowCount)*100 ;
    float f = (float)Math.round(percent);
    s+=17;
    textFont(fontB, 18);
    text(f+"% \t", width-100,40+s);
    text(me.getKey()+" ["+me.getValue()+"]",width-450,40+s);
  }
  textFont(fontA, 32);
  text(((rowCount-1)+" Total lines"),width-290,height-15); // subtract one for the title column
}

public void crunchData(int col){
  rowCount = t.getRowCount(  );
  title = t.getString(0, col);

  for (int i = 1; i < rowCount; i++) { // Start at 1 to skip the col title
    String value = t.getString(i, col);

    if(tm.containsKey(value)){
      count = (Integer)tm.get(value);
      tm.put(value,++count);
    }
    else if(!tm.containsKey(value)){
      tm.put(value,1);
    }     
  }  
}

public void crunchData(int col, int col2){
  rowCount = t.getRowCount(  );
  title = t.getString(0, col);
  title2 = t.getString(0,col2);

  for (int i = 1; i < rowCount; i++) { // Start at 1 to skip the col title
    String value = t.getString(i, col2);

    if(tm.containsKey(value)){
      time = (Integer)tm.get(value);
      tm.put(value,++time);
    }
    else if(!tm.containsKey(value)){
      time = (Integer)tm.get(value);
      tm.put(value,time);
    }     
  }  
}

public void keyPressed() {
  if (key == '[') {
    column--;
    if (column < 0) {
      column = t.getRowLength() - 1;
    }
  } 
  else if (key == ']') {
    column++;
    if (column == t.getRowLength()) {
      column = 0;
    }
  }
  else if (key == 'p'){
    printMapContents(tm);
  }
  else if (key == 'c'){
    println();
    println();
    println();
  }  
}

public void printMapContents(TreeMap<String, Integer> tm){
  println(t.getString(0,column));
  Iterator i = tm.entrySet().iterator();  // Get an iterator
  while (i.hasNext()){
    Map.Entry me = (Map.Entry)i.next();
    Integer value = (Integer)me.getValue(); // Retrieve the value as Integer (obj)
    percent = ((float)value/(float)rowCount)*100 ;
    float f = (float)Math.round(percent);
    print(f+"% \t");
    print(me.getKey()+" ["+me.getValue()+"]");
    println();
  }
  println(((rowCount-1)+" Total lines")); // subtract one for the title column
}
/* 
 Based off of some great example code from Quark
 http://processing.org/discourse/yabb2/YaBB.pl?num=1251333472/9 
 
 treeMap example
 http://www.java2s.com/Code/Java/Collections-Data-Structure/SortingElementsinaTreeMap.htm
 */


class Table {
  String[][] data;
  int rowCount;
  
  Table() {
    data = new String[10][10];
  }

  Table(String filename) {
    String[] rows = loadStrings(filename);
    data = new String[rows.length][];
    
    for (int i = 0; i < rows.length; i++) {
      data[rowCount++] = split(rows[i], TAB);
    }
    // resize the 'data' array as necessary
    data = (String[][]) subset(data, 0, rowCount);
  }

  public int getRowCount() {
    return rowCount;
  }
  
  public int getRowLength(){
    return data[0].length;
  }
  // find a row by its name, returns -1 if no row found
  public int getRowIndex(String name) {
    for (int i = 0; i < rowCount; i++) {
      if (data[i][0].equals(name)) {
        return i;
      }
    }
    println("No row named '" + name + "' was found");
    return -1;
  }
  
  public String getRowName(int row) {
    return getString(row, 0);
  }

  public String getString(int rowIndex, int column) {
    return data[rowIndex][column];
  }
  
  public String getString(String rowName, int column) {
    return getString(getRowIndex(rowName), column);
  }
  
  public int getInt(String rowName, int column) {
    return parseInt(getString(rowName, column));
  }
  
  public int getInt(int rowIndex, int column) {
    return parseInt(getString(rowIndex, column));
  }
  
  public float getFloat(String rowName, int column) {
    return parseFloat(getString(rowName, column));
  }
  
  public float getFloat(int rowIndex, int column) {
    return parseFloat(getString(rowIndex, column));
  }
 
  public void setRowName(int row, String what) {
    data[row][0] = what;
  }

  public void setString(int rowIndex, int column, String what) {
    data[rowIndex][column] = what;
  }
  
  public void setString(String rowName, int column, String what) {
    int rowIndex = getRowIndex(rowName);
    data[rowIndex][column] = what;
  }
  
  public void setInt(int rowIndex, int column, int what) {
    data[rowIndex][column] = str(what);
  }
  
  public void setInt(String rowName, int column, int what) {
    int rowIndex = getRowIndex(rowName);
    data[rowIndex][column] = str(what);
  }
  
  public void setFloat(int rowIndex, int column, float what) {
    data[rowIndex][column] = str(what);
  }

  public void setFloat(String rowName, int column, float what) {
    int rowIndex = getRowIndex(rowName);
    data[rowIndex][column] = str(what);
  }
  
  // Write this table as a TSV file
  public void write(PrintWriter writer) {
    for (int i = 0; i < rowCount; i++) {
      for (int j = 0; j < data[i].length; j++) {
        if (j != 0) {
          writer.print(TAB);
        }
        if (data[i][j] != null) {
          writer.print(data[i][j]);
        }
      }
      writer.println();
    }
    writer.flush();
  }
}

  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#d4d0c8", "tmapData3" });
  }
}
